<?php
class upcomingevents_count extends WP_Widget
{
  function upcomingevents_count()
  {
    $widget_ops = array('classname' => 'upcomingevents_count small-banners', 'description' => 'Select Event to show its countdown.' );
    $this->WP_Widget('upcomingevents_count', 'ChimpS : Event Countdown', $widget_ops);
  }
 
  function form($instance)
  {
    $instance = wp_parse_args( (array) $instance, array( 'title' => '' ,'widget_names_events' =>'new') );
    $title = $instance['title'];
	//$img_url = isset( $instance['img_url'] ) ? esc_attr( $instance['img_url'] ) : '';
	$get_names_events = isset( $instance['get_names_events'] ) ? esc_attr( $instance['get_names_events'] ) : '';
?>
  <p>
  <label for="<?php echo $this->get_field_id('title'); ?>">
	  Title: 
	  <br />
	  <input class="upcoming" id="<?php echo $this->get_field_id('title'); ?>" size="40" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
  </label>
  </p>
	<!--<p>
		<label for="<?php echo $this->get_field_id('img_url'); ?>">
			  Background Image Url:
			  <br />
			  <input class="upcoming" id="<?php echo $this->get_field_id('img_url'); ?>" size="40" name="<?php echo $this->get_field_name('img_url'); ?>" type="text" value="<?php echo esc_attr($img_url); ?>" />
		</label>
	</p>-->  
  <p>
  <label for="<?php echo $this->get_field_id('get_names_events'); ?>">
	  Select Event:
	  <select id="<?php echo $this->get_field_id('get_names_events'); ?>" name="<?php echo $this->get_field_name('get_names_events'); ?>" style="width:225px;">
		<?php
        global $wpdb,$post;
        $newpost = 'posts_per_page=-1&post_type=events&order=ASC&post_status=publish';
			$newquery = new WP_Query($newpost);
				while ( $newquery->have_posts() ): $newquery->the_post(); ?>
                    <option <?php if(esc_attr($get_names_events) == $post->ID){echo 'selected';}?> value="<?php echo $post->ID;?>" >
	                    <?php echo substr(get_the_title($post->ID), 0, 20);	if ( strlen(get_the_title($post->ID)) > 20 ) echo "...";?>
                    </option>						
			<?php endwhile;?>
      </select>
  </label>
  </p>  
<?php
  }
 
  function update($new_instance, $old_instance)
  {
    $instance = $old_instance;
    $instance['title'] = $new_instance['title'];
	//$instance['img_url'] = $new_instance['img_url'];
	$instance['get_names_events'] = $new_instance['get_names_events'];	
    return $instance;
  }
 
	function widget($args, $instance)
	{
		extract($args, EXTR_SKIP);


		$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
		//$img_url = isset( $instance['img_url'] ) ? esc_attr( $instance['img_url'] ) : '';
		$get_names_events = isset( $instance['get_names_events'] ) ? esc_attr( $instance['get_names_events'] ) : '';		
		if(!isset($get_names_events)){$get_names_events = '4';}
		// WIDGET display CODE Start
		echo $before_widget;		
		if (!empty($title))
			//echo $before_title . $title . $after_title;
			global $wpdb, $post;
							$cs_event_from_date = get_post_meta($get_names_events, "cs_event_from_date", true);
							//$event_to_date = get_post_meta($instance['get_names_events'], "cs_event_to_date", true);
							$year_event = date("Y", strtotime($cs_event_from_date));
							$month_event = date("m", strtotime($cs_event_from_date));
							$month_event_c = date("M", strtotime($cs_event_from_date));							
							$date_event = date("d", strtotime($cs_event_from_date));
							$cs_event_meta = get_post_meta($post->ID, "cs_event_meta", true);
							if ( $cs_event_meta <> "" ) {
								$cs_event_meta = new SimpleXMLElement($cs_event_meta);
							}
							$image_id = get_post_thumbnail_id ( $get_names_events );
							if($image_id <> ''){
								$image_url = cs_attachment_image_src($image_id, 438, 288);
							}
							?>
				<script type="text/javascript" src="<?php echo get_template_directory_uri();?>/scripts/jquery.countdown.js"></script>
                <script>
					$(function () {
							var austDay = new Date();
							austDay = new Date(<?php echo $year_event;?>, <?php echo $month_event;?>-1, <?php echo $date_event;?>)
							$('#defaultCountdownn').countdown({until: austDay});
							$('#year').text(austDay.getFullYear());
					});                
                </script>
                        <a href="#" class="thumb"><?php if(isset($image_url)){?><img width="298" height="188" src="<?php echo $image_url?>" alt="" /><?php }else{?><img src="<?php echo get_template_directory_uri(); ?>/images/img1.jpg" width="298" height="188" alt="" /><?php }?></a>
                        <h1 class="title"><?php echo substr($title, 0, 20); if ( strlen($title) > 20 ) echo "...";?></h1>
                        <div class="event-counter">
                            <?php if($get_names_events <> ''){?>
                            <h3>
                            	<a class="colr" href="<?php echo get_permalink($get_names_events);?>">
									<?php 
										echo substr( get_the_title ($get_names_events), 0, 15 );
										$count_len = strlen( get_the_title ($get_names_events) );
										if ( $count_len > 15 ) echo "...";
									?>
	                            </a>
							</h3>
                            <?php }?>
                            <div id="defaultCountdownn"></div>
						</div>
                <?php
			echo $after_widget;	// WIDGET display CODE End
		}
	}
add_action( 'widgets_init', create_function('', 'return register_widget("upcomingevents_count");') );?>